__author__ = 'gsoft2-3'

#coding=utf-8
import  xml.dom.minidom
import re
import os
import shutil

def get_xmlnode(node, name):
    return node.getElementsByTagName(name) if node else []

def get_attrvalue(node, attrname):
     return node.getAttribute(attrname) if node else ''

def get_nodevalue(node, index = 0):
    try:
        return node.childNodes[index].nodeValue if node else ''
    except:
        return ''


def compare(path0,path1):
    ###This is path0
    try:
        dom0 = xml.dom.minidom.parse(path0)
    except:
        print "can not find file path0===>>>"+path0
        return
    root0 = dom0.documentElement
    string_nodes0 = get_xmlnode(root0, 'string')
    #print "string_nodes:", string_nodes
    string_list_path0=[]
    for node0 in string_nodes0:
        name = get_attrvalue(node0,'name')
        myvalue = get_nodevalue(node0)

        mystring = {}
        mystring['name'] , mystring['myvalue'] = (
        name , myvalue
        )
        string_list_path0.append(mystring)

    #for mynode in string_list_path0:
    #    print mynode['myvalue']

    #This is path1
    try:
        dom1 = xml.dom.minidom.parse(path1)
    except:
        if os.path.exists(path1):
            print "err xml file .please check file:"+path1
            return
        else:
            if os.path.exists(path0):
                dirpath=path1.replace("strings.xml","")
                if not os.path.exists(dirpath):
                    os.mkdir(dirpath)
                shutil.copy(path0,path1)
                print "path1 not exit:"+path1
                return
        return
    root1 = dom1.documentElement
    string_nodes1 = get_xmlnode(root1, 'string')
    string_list_path1=[]
    for node in string_nodes1:
        name = get_attrvalue(node,'name')
        myvalue = get_nodevalue(node)

        mystring = {}
        mystring['name'] , mystring['myvalue'] = (
        name , myvalue
        )
        string_list_path1.append(mystring)


    stringBuffer = ""
    list1have = False
    for mynode0 in string_list_path0:
        #print "compare "+mynode0['name']
        for mynode1 in string_list_path1:
            if mynode0['name'] == mynode1['name']:
                #print "both have =========>>>>>>>>"+mynode0['name']
                list1have=True
                continue

        if list1have == False:
            #<string name="application_namepp">XXXLauncher3</string>
            #print "<string name=" + '\"' + mynode0['name']+'\">'+ mynode0['name']+'</string>'
            stringBuffer = stringBuffer+'\n'+"<string name=" + '\"' + mynode0['name']+'\">'+ mynode0['name']+'</string>'
        list1have=False

    print stringBuffer

    lines = []
    f=open(path1,'r')
    for line in f.readlines():
        if re.search("</resources>",line):
            #line.replace("</resources>","")
            line=line.replace("</resources>","")
        lines.append(line)
    f.close()


    f=open(path1,'w')
    f.writelines(lines)
    f.write(stringBuffer)
    f.write('\n'+"</resources>")
    f.close()